// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage from "../manage/AudioManage";
import config from "../config/config";
import Sound from "./Sound";
import func from "../config/func";
import Ranking from "./Ranking";
import AdManage from "../manage/AdManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartGame extends cc.Component {

    @property(cc.Sprite)
    m_robot: cc.Sprite = null;
    @property(cc.SpriteFrame)
    m_robotFrame: cc.SpriteFrame[] = [];
    @property(cc.Node)
    m_view: cc.Node = null;

    n_robotAni: cc.Tween = null;
    n_rank: cc.Node = null;
    n_share: cc.Node = null;

    onLoad() {
        this.n_rank = this.node.getChildByName("btn_rank");
        this.n_share = this.node.getChildByName("btn_share");
    }

    start() {
        this.n_robotAni = cc.tween(this.m_robot.node)
            .repeatForever(
                cc.tween()
                    .to(2.5, { x: 230 })
                    .call(() => {
                        this.m_robot.spriteFrame = this.m_robotFrame[1];
                    })
                    .to(2.5, { x: -230 })
                    .call(() => {
                        this.m_robot.spriteFrame = this.m_robotFrame[0];
                    })
            )
        this.n_robotAni.start();
    }

    init() {
        if (config.gameType == "wx") {
            this.n_rank.active = true;
            this.n_share.active = true;
        } else {
            this.n_rank.active = false;
            this.n_share.active = false;
        }
        if (config.gameType == "vivo") {
            func.creatrBanner()
        } else if (config.gameType == "QQ") {
            AdManage.instance.initQQcreateBlockAd("landscape");
            config.sclY = AdManage.instance.n_systemInfo.screenHeight / AdManage.instance.n_systemInfo.screenWidth;
            func.creatrBanner();
        }
    }

    /**
     * 开始游戏
     */
    startGame() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (config.gameType == "QQ") {
            AdManage.instance.n_blockAd && AdManage.instance.n_blockAd.destroy();
            func.destroyBanner();
        }
        this.n_robotAni.stop();
        cc.director.loadScene("game");
    }
    /**
    * 点击分享按钮
    */
    touchShareBtn() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (config.gameType == "wx") {
            window["wx"].shareAppMessage({
                title: '我要当主厨！'
            })
        }
    }
    /**
     * 打开排行榜
     */
    async touchRank() {
        if (config.gameType == "wx") {
            AudioManage.audioManage.playEffectMusic(config.soundPath.click);
            config.openPopup = true;
            let rank = this.m_view.getChildByName("rank");
            if (rank) {
                rank.active = true;
                rank.getComponent(Ranking).init();
            } else {
                let child: cc.Node = null
                if (config.prefabArr[config.prefabPath.rank]) {
                    child = cc.instantiate(config.prefabArr[config.prefabPath.rank]);
                } else {
                    let prefab = await func.loadResouce(config.prefabPath.rank, cc.Prefab) as cc.Prefab;
                    child = cc.instantiate(prefab);
                    config.prefabArr[config.prefabPath.rank] = prefab;
                }
                child.parent = this.m_view;
                child.getComponent(Ranking).init();
            }
        }
    }
    /**
     * 点击设置按钮
     */
    async touchSetBtn() {
        if (config.openPopup) return;
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        config.openPopup = true;
        let soundNode = this.m_view.getChildByName("sound");
        if (soundNode) {
            soundNode.active = true;
            soundNode.getComponent(Sound).init();
        } else {
            let child: cc.Node = null
            if (config.prefabArr[config.prefabPath.setPopup]) {
                child = cc.instantiate(config.prefabArr[config.prefabPath.setPopup]);
            } else {
                let prefab = await func.loadResouce(config.prefabPath.setPopup, cc.Prefab) as cc.Prefab;
                child = cc.instantiate(prefab);
                config.prefabArr[config.prefabPath.setPopup] = prefab;
            }
            child.parent = this.m_view;
            child.getComponent(Sound).init();
        }
    }
}
